package com.example.lottery.exception;

import com.example.lottery.enums.Message;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @title: GlobalExceptionHandler
 * @projectName: HO-CE
 * @description: 异常统一处理
 * @author: tanwei
 * @date: 2021/12/283:55 下午
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @Autowired
    HttpServletRequest httpServletRequest;


    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Message error(Exception e) {
        log.error("系统异常信息--%-- 请求路径--% {} --%用户名--% {} ", httpServletRequest.getRequestURI(), httpServletRequest.getHeader("token"), e);
        return new Message().error();
    }


    @ExceptionHandler(BadSqlGrammarException.class)
    @ResponseBody
    public Message error(BadSqlGrammarException e) {
        log.error("sql异常信息--%-- 请求路径--% {} --%用户名--% {} ", httpServletRequest.getRequestURI(), httpServletRequest.getHeader("token"), e);
        return new Message().error();
    }


    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public Message error(BusinessException e) {
        log.error("业务异常信息--%-- 请求路径--% {} --%用户名--% {} ", httpServletRequest.getRequestURI(), httpServletRequest.getHeader("token"), e);
        return new Message().error(e.getCode(), e.getMessage());
    }
}
